import urllib, urllib2, re, xbmcplugin, xbmcgui, os, cookielib

#.nF0
__plugin__ = "Apina"
__author__ = "Anders Brujordet"
__date__ = "06 01 2010"
__version__ = "1"

handle = sys.argv[1]
dialog = xbmcgui.Dialog()
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
user_agent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0'
headers = { 'User-Agent' : user_agent }
values = {'name' : 'Michael Foord','location' : 'Northampton','language' : 'Python' }
data = urllib.urlencode(values)
url = "http://apina.biz/?i_really=need_it&ruri=%2F"

req = urllib2.Request(url, data, headers)
response = opener.open(req)

LIMIT = 20
counter = 1
linkregex = re.compile('[\'|"](.*?)[\'"].*?')

while counter < LIMIT:
    url = "http://apina.biz/random"
    req = urllib2.Request(url, data, headers)
    response = opener.open(req)
    links = linkregex.findall(response.read())
    
    for pic in links:
        if "weisswurst" in pic and "gif" not in pic:
            listitem = xbmcgui.ListItem( os.path.basename(pic), iconImage="DefaultPicture.png", thumbnailImage=pic)
            listitem.setInfo( type="Picture", infoLabels={"Title": os.path.basename(pic)})
            xbmcplugin.addDirectoryItem( handle=int(handle), url=pic, listitem=listitem, isFolder=False, totalItems=(LIMIT))
    counter += 1

xbmcplugin.addSortMethod( handle=int(handle), sortMethod=xbmcplugin.SORT_METHOD_NONE)
xbmcplugin.endOfDirectory(int(handle))
